<template>
   <el-dialog 
    title="添加用户" 
    :visible.sync="dialogVisible" 
    width="50%" 
    @close="diologClose"
    :close-on-click-modal="false">

    <!-- form表单 -->
    <el-form
      :model="permissionForm"
      :rules="rules"
      ref="addPermissionFormRef"
      label-width="100px">

      <!-- 用户名 -->
      <el-form-item label="权限名" prop="permissionName">
        <el-input v-model="permissionForm.permissionName"></el-input>
      </el-form-item>

      <!-- 密码 -->
      <el-form-item label="权限url" prop="url">
        <el-input v-model="permissionForm.url"></el-input>
      </el-form-item>

    </el-form>

    <!-- 底部 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogFormCheckFunc">确 定</el-button>
    </span>

  </el-dialog>
</template>

<script>
// 请求
import { postAddPermission } from '@/network/rightControlRequest.js';

export default {
  data() {
    return {
      dialogVisible: false,
      permissionForm: {
        permissionName: '',
        url: ''
      },
      rules:{
        permissionName: [{ required: true, message: '请输入权限名', trigger: 'blur' }],
        url: [{ required: true, message: '请输入url', trigger: 'blur' }],
      }
    }
  },
  methods: {
    /**
     * 关闭dialog回调
     */
    diologClose() {
      this.$refs['addPermissionFormRef'].clearValidate();
    },

    /**
     * 提交表单
     */
    async dialogFormCheckFunc() {
      const ret = await this.$refs['addPermissionFormRef'].validate().catch(e => e);
      if (!ret) {return this.$message.error('请将表单填写完整');}
      
      // 请求
      const {meta} = await postAddPermission(this.permissionForm);
      if (meta.status != 200) {return this.$message.error(meta.msg);}

      // 成功
      this.$bus.$emit('getRightList');
      this.$refs['addPermissionFormRef'].resetFields();
      this.dialogVisible = false;
    },

    /**
     * 开启dialog
     */
    openDialog() {
      this.dialogVisible = true;
    }
  },
  created() {
    this.$bus.$on('openDialog', this.openDialog);
  },
  destroyed() {
    this.$bus.$off('openDialog');
  }
}
</script>

<style>

</style>